﻿using System;
using System.Collections.Generic;
using System.Text;
using WebsiteDataModel;
using System.Linq;
using System.IO;
using System.Web;

namespace BLL
{
    /// <summary>
    ///NewStockBLL 的摘要说明
    /// </summary>
    public class NewStockBLL
    {
        public NewStockBLL()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        #region 新股详细
        /// <summary>
        /// 新股详细
        /// </summary>
        /// <param name="stockcode">股票代码</param>
        /// <returns></returns>
        public static string XgDetail(string stockcode)
        {
            WebsiteDataEntities de = new WebsiteDataEntities();
            var re = de.XG_Detail.Where(a => a.gpdm == stockcode);
            StringBuilder result = new StringBuilder();
            if (re.Count() > 0)
            {
                XG_Detail node = re.ToArray()[0];
                result.AppendLine("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"1\" border=\"0\">");
                result.AppendLine("  <tr>");
                result.AppendFormat("<th colspan=\"4\"> {0}（{1}）新股详细资料</th>\r\n", node.gpjc, node.gpdm);
                result.AppendLine("  </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td width=\"221\" class=\"right\">股票代码</td>");
                result.AppendFormat("      <td width=\"228\" class=\"left\">{0}</td>\r\n", node.gpdm);
                result.AppendLine("      <td width=\"239\" class=\"right\">股票简称</td>");
                result.AppendFormat("      <td width=\"242\" class=\"left\">{0}</td>\r\n", node.gpjc);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">申购代码</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", node.sgdm);
                result.AppendLine("      <td class=\"right\">上市地点</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", node.ssdd);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">发行价格（元／股）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.fxjg, "0.00"));
                result.AppendLine("      <td class=\"right\">发行市盈率</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.fxsyl, "0.00"));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">发行面值（元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.fxmz, "0.00"));
                result.AppendLine("      <td class=\"right\">实际募集资金总额（亿元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.fxmz, "0.00"));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">网上发行日期</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDateTime(node.fxrq, "yyyy-MM-dd"));
                result.AppendLine("      <td class=\"right\">网上发行股数（股）</td>");
                result.AppendFormat("    <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringInt(node.fxgs));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">网下配售日期</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDateTime(node.psrq, "yyyy-MM-dd"));
                result.AppendLine("      <td class=\"right\">网下配售股数（股）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringInt(node.psgs));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">申购数量上限（股）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringInt(node.sgsl));
                result.AppendLine("      <td class=\"right\">总发行股数（股）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringInt(node.zfxgs));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td colspan=\"4\">&nbsp;</td>");
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">中签号公告日</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDateTime(node.zqhggr, "yyyy-MM-dd"));
                result.AppendLine("      <td class=\"right\">上市日期</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDateTime(node.ssrq, "yyyy-MM-dd"));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">网下配售比例（％）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.psbl, "0.00"));
                result.AppendLine("      <td class=\"right\">网下配售冻结资金（亿元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.psdjzj, "0.00"));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">网上发行中签率（％）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.fxzql, "0.00"));
                result.AppendLine("      <td class=\"right\">网上申购冻结资金（亿元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.sgdjzj, "0.00"));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">网上发行每中一签约（万元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.fxqy, "0.00"));
                result.AppendLine("      <td class=\"right\">冻结资金总计（亿元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.Djzjzj, "0.00"));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td>中签号</td>");
                result.AppendFormat("      <td colspan=\"3\" class=\"left\">{0}</td>\r\n", node.zqh);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td colspan=\"4\">&nbsp;</td>");
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td>公司简介</td>");
                result.AppendFormat("      <td colspan=\"3\" class=\"left\">{0}</td>\r\n", node.gsjj);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td>主营业务</td>");
                result.AppendFormat("      <td colspan=\"3\" class=\"left\">{0}</td>\r\n", node.zyyw);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td colspan=\"4\">&nbsp;</td>");
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">发行前每股净资产（元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.fxq, "0.00"));
                result.AppendLine("      <td class=\"right\">发行后每股净资产（元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.fxh, "0.00"));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">上市推荐人</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", node.tjr);
                result.AppendLine("      <td class=\"right\">承销方式</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", node.cxfs);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">主承销商</td>");
                result.AppendFormat("      <td colspan=\"3\" class=\"left\">{0}</td>\r\n", node.zcxs);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">副主承销商</td>");
                result.AppendFormat("      <td colspan=\"3\" class=\"left\">{0}</td>\r\n", node.fzcxs);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">股利分配政策</td>");
                result.AppendFormat("      <td colspan=\"3\" class=\"left\">{0}</td>\r\n", node.glfpzc);
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">首日开盘价（元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.kpj, "0.00"));
                result.AppendLine("      <td class=\"right\">首日收盘价（元）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.spj, "0.00"));
                result.AppendLine("    </tr>");
                result.AppendLine("    <tr>");
                result.AppendLine("      <td class=\"right\">首日涨幅（％）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.zf, "0.00"));
                result.AppendLine("      <td class=\"right\">首日换手率（％）</td>");
                result.AppendFormat("      <td class=\"left\">{0}</td>\r\n", DAL.NumFormat.ConvertStringDecimal(node.hsl, "0.00"));
                result.AppendLine("    </tr>");
                //result.AppendLine("    <tr>");
                //result.AppendLine("      <td class=\"right\">打新收益率（％）</td>");
                //result.AppendFormat("      <td class=\"left\">{0}</td>", DAL.NumFormat.ConvertStringDecimal(node.fxq, "0.00"));
                //result.AppendLine("      <td>&nbsp;</td>");
                //result.AppendLine("      <td class=\"left\">&nbsp;</td>");
                //result.AppendLine("    </tr>");
                result.AppendLine("</table>");
            }

            return result.ToString();
        }
        #endregion

        #region 新股IPO日程
        /// <summary>
        /// 新股IPO日程公告
        /// </summary>
        /// <param name="stockcode"></param>
        /// <returns></returns>
        public static string XgGgrq(string stockcode)
        {
            WebsiteDataEntities de = new WebsiteDataEntities();
            var re1 = de.XG_Detail.Where(a => a.gpdm == stockcode).Select(a => new { a.id, a.gpjc }).ToArray();
            StringBuilder result = new StringBuilder();
            foreach (var tt in re1)
            {
                //int deid = tt.id;
                var re = de.XG_Report.Where(a => a.code.Equals(stockcode));

                result.AppendLine("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"1\" border=\"0\">");
                result.AppendLine("  <tr>");
                result.AppendFormat("    <th colspan=\"4\"> {0}IPO初步询价及推介公告日程 </th>", tt.gpjc);
                result.AppendLine("  </tr>");
                result.AppendLine("  <tr class=\"ym_em_thl2\">");
                result.AppendLine("    <th>序号</th>");
                result.AppendLine("    <th>日期</th>");
                result.AppendLine("    <th>发行安排</th>");
                result.AppendLine("  </tr>");
                int i = 0;
                foreach (var gg in re)
                {
                    i++;
                    result.AppendLine("  <tr>");
                    result.AppendFormat("    <td>{0}</td>\r\n", i);
                    result.AppendFormat("    <td>{0}</td>\r\n", gg.sdate);
                    result.AppendFormat("    <td>{0}/td>\r\n", gg.detail);
                    result.AppendLine("  </tr>");
                }
                result.AppendLine("</table>");
            }
            return result.ToString();
        }
        #endregion

        #region IPO募集资金用途将用于的项目
        public static string XgInvset(string stockcode) 
        {
            WebsiteDataEntities de = new WebsiteDataEntities();
            var re1 = de.XG_Detail.Where(a => a.gpdm == stockcode).Select(a => new { a.id, a.gpjc }).ToArray();
            StringBuilder result = new StringBuilder();
            foreach (var tt in re1)
            {
                int deid = tt.id;
                var re = de.XG_Invset.Where(a => a.id == deid);
                result.AppendLine("<table cellspacing=\"1\" cellpadding=\"1\">");
                result.AppendLine("    <tbody>");
                result.AppendLine("      <tr>");
                result.AppendFormat("        <th colspan=\"4\"> {0}IPO募集资金用途将用于的项目 </th>", tt.gpjc);
                result.AppendLine("      </tr>");
                result.AppendLine("      <tr class=\"ym_em_thl2\">");
                result.AppendLine("        <th>序号</th>");
                result.AppendLine("        <th>项目名称</th>");
                result.AppendLine("        <th>投资额（万元）</th>");
                result.AppendLine("      </tr>");
                int i = 0;
                foreach (XG_Invset gg in re)
                {
                    i++;
                    result.AppendLine("      <tr>");
                    result.AppendFormat("        <td>{0}</td>\r\n", i);
                    result.AppendFormat("        <td>{0}</td>\r\n", gg.xmmc);
                    result.AppendFormat("        <td>{0}</td>\r\n", DAL.NumFormat.ConvertStringInt(gg.tzje));
                    result.AppendLine("      </tr>");
                }
                result.AppendLine("    </tbody>");
                result.AppendLine("</table>");
            }

            return result.ToString();
        }
        #endregion

        #region 新股主要股东
        public static string XgPartner(string stockcode)         
        {
            WebsiteDataEntities de = new WebsiteDataEntities();
            var re1 = de.XG_Detail.Where(a => a.gpdm == stockcode).Select(a => new { a.id, a.gpjc }).ToArray();
            StringBuilder result = new StringBuilder();
            foreach (var tt in re1)
            {
                int deid = tt.id;
                var re = de.XG_Partner.Where(a => a.id == deid);
                result.AppendLine("<table cellspacing=\"1\" cellpadding=\"1\">");
                result.AppendLine("    <tbody>");
                result.AppendLine("      <tr>");
                result.AppendFormat("        <th colspan=\"4\">{0}主要股东 </th>", tt.gpjc);
                result.AppendLine("      </tr>");
                result.AppendLine("      <tr class=\"ym_em_thl2\">");
                result.AppendLine("        <th>序号</th>");
                result.AppendLine("        <th>股东名称</th>");
                result.AppendLine("        <th>持股数量</th>");
                result.AppendLine("        <th>占总股本比例</th>");
                result.AppendLine("      </tr>");
                int i = 0;
                foreach (XG_Partner gg in re)
                {
                    i++;
                    result.AppendLine("      <tr>");
                    result.AppendFormat("        <td>{0}</td>",i);
                    result.AppendFormat("        <td>{0}</td>",gg.gdmc);
                    result.AppendFormat("        <td>{0}</td>", DAL.NumFormat.ConvertStringInt(gg.cgsl));
                    result.AppendFormat("        <td>{0}</td>", DAL.NumFormat.ConvertStringDecimal(gg.gbbl));
                    result.AppendLine("      </tr>");
                }
                result.AppendLine("    </tbody>");
                result.AppendLine("  </table>");
            }

            return result.ToString();
        }
        #endregion

        #region 新股公告信息
        public static string XgGg(string stockcode) 
        {
            WebsiteDataEntities de = new WebsiteDataEntities();
            var tt = de.XG_GG.Where(a => a.gpdm == stockcode).OrderByDescending(a => a.ggrq);

            StringBuilder result = new StringBuilder();
            result.AppendLine("<table cellspacing=\"1\" cellpadding=\"1\">");
            result.AppendLine("    <tbody class=\"left\">");
            result.AppendLine("      <tr>");
            result.AppendFormat("        <th>最新公告</th>");
            result.AppendLine("      </tr>");
            foreach (XG_GG gg in tt)
            {
                result.AppendLine("      <tr>");
                string _url = "http://down3.emstock.com.cn/xg/gg/" + gg.id + ".htm";
                result.AppendFormat("        <td><a href=\"{0}\">{1}</a> <span>{2}</span></td>", _url, gg.title, DAL.NumFormat.ConvertStringDateTime(gg.ggrq, "yyyy-MM-dd"));
                result.AppendLine("      </tr>");
            }
            result.AppendLine("    </tbody>");
            result.AppendLine("</table>");



            return result.ToString();
        }
        #endregion
    }
}